package com.peng.leetcode.search;

/**
 * Search1
 * 704. 二分查找
 * https://leetcode.cn/problems/binary-search/
 * <p>
 * Created on 2022/12/9
 *
 * @author lupeng
 */
public class Search1 {

    public static void main(String[] args) {
        int search = new Search1().search(new int[]{1, 4, 7, 11, 15}, 5);
        System.out.println(search);
    }

    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return -1;
    }
}
